package cn.wizzer.app.web.modules.controllers.platform.crown;


import cn.wizzer.app.crown.modules.models.CrownLeague;
import cn.wizzer.app.crown.modules.models.CrownLeagueResult;
import cn.wizzer.app.crown.modules.services.CrownLeagueService;
import cn.wizzer.app.crown.modules.services.result.LeagueResultService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.Date;
import java.util.List;

@IocBean
@At("/platform/crown/league")
public class CrownLeagueController {

    @Reference
    @Inject
    private CrownLeagueService leagueService;

    @Reference
    @Inject
    private LeagueResultService leagueResultService;

    @Inject
    private RedisService redisService;


    @At("/list")
    @Ok("beetl:/platform/crown/league/list.html")
    @RequiresPermissions("crown.league.list")
    public void index() {
    }

    @At("/edit")
    @Ok("json")
    @RequiresPermissions("crown.league.list")
    public Object edit(@ApiParam(required = true, value = "id") @Param("id") String id, @Param("oldVersion") Integer oldVersion) {
        CrownLeague league = leagueService.fetch(id);
        Cnd cnd = Cnd.where("league_id", "=", id);
        // 如果传入的参数为空，按最新的版本查
        if (null == oldVersion) {
            oldVersion = league.getVersion();
        }
        cnd.and("version", "=", oldVersion);
        List<CrownLeagueResult> list = leagueResultService.query(cnd);
        league.setSubResults(list);
        return Result.success().addData(league);
    }

    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("crown.league.edit")
    @SLog(tag = "修改信息")
    public Object editDo(@Param("..") CrownLeague league) {
        leagueService.update(league);
        return Result.success();
    }

    /**
     * 根据分类信息查询赛事信息
     *
     * @param start
     * @param end
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @At("/data")
    @Ok("json:full")
    @RequiresPermissions("crown.league.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("matchType") String matchType,
                       @Param("stage") String stage,
                       @Param("start")Date start,
                       @Param("end") Date end,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        Cnd cnd = Cnd.NEW();
        if (start != null) {
            cnd.and("date_time",">=",start);
        }
        if (end != null) {
            cnd.and("date_time","<",end);
        }
        if (Strings.isNotBlank(matchType)) {
            cnd.and("type", "=", matchType);
        }
        if (Strings.isNotBlank(stage)) {
            cnd.and("stage", "=", stage);
        }
        if (Strings.isNotBlank(searchKeyword)) {
            SqlExpressionGroup e1 = Cnd.exps("champion_name", "like", "%" + searchKeyword + "%");
            SqlExpressionGroup e2 = Cnd.exps("league", "like", "%" + searchKeyword + "%");
            cnd.and(e1.or(e2));
        }
        Pagination pagination = leagueService.listPage(pageNumber, pageSize, cnd.orderBy("date_time","desc"));
        return Result.success(pagination);
    }


    /**
     * 手动结算冠军订单
     *
     * @return
     */
    @At("/settleChampion")
    @Ok("json")
    @RequiresPermissions("crown.league.edit")
    public Object settleChampion(@Param("..") CrownLeague league){
        leagueService.settleChampion(league);
        return Result.success();
    }


    /**
     * 手动爬取联赛赛果
     *
     * @param searchDate
     * @return
     */
    @At("/crawlLeagueResult")
    @Ok("json")
    @RequiresPermissions("crown.league.edit")
    @SLog(tag = "爬取联赛赛果")
    public Object crawlMatchResult(@Param("searchDate") String[] searchDate) {
        if (null == searchDate || searchDate.length == 0) {
            return Result.error("请选择要爬取的日期");
        }
        // 转毫秒
        Long startTime = Times.d2TS(Times.D(searchDate[0])) * 1000;
        Long endTime = Times.d2TS(Times.D(searchDate[1])) * 1000;
        leagueService.crawlLeagueResult(startTime, endTime);
        return Result.success();
    }
}
